//
//  GlobalUnit.m
//  EastMarket
//
//  Created by 李鹏程 on 16/10/12.
//  Copyright © 2016年 guoguo. All rights reserved.
//

#import "GlobalUnit.h"

@implementation GlobalUnit

static GlobalUnit *_manager = nil;


+ (GlobalUnit *) shareManager
{
    
    //    @synchronized(self){
    if(!_manager){
        _manager = [[GlobalUnit alloc] init];
    }
    //    }
    return _manager;
}

+ (BOOL) isBlankString:(NSString *)string {
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}

+(NSString *) jsonStringWithString:(NSString *) string{
    return [NSString stringWithFormat:@"%@",[[string stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"] stringByReplacingOccurrencesOfString:@""withString:@"\\"]];
}

+(NSInteger)getIntValue:(id)object
{
    return [self getIntValue:object withDefault:-1];
}

+(NSInteger)getIntValue:(id)object withDefault:(NSInteger)defaultvalue
{
    if (!object) {
        return defaultvalue;
    }
    return [object intValue];
}

//有些对象从json中取出来是nil，导致符给label的text失败，这里封装成函数
+(NSString *)getStringValue:(id)object
{
    return [self getStringValue:object withDefault:@""];
}

+(NSString *)getStringValue:(id)object withDefault:(NSString*)defaultvalue
{
    if (!object) {
        return defaultvalue;
    }
    return [NSString stringWithFormat:@"%@", object];
}
+(void)showAlertHint:(NSString*)title message:(NSString*)message
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title  message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
}

+(void)showMessage:(NSString *)message {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            alerView.backgroundColor = EastMarket_hintFontColor;
            [alerView show];
            [alerView performSelector:@selector(dismissWithClickedButtonIndex:animated:) withObject:@[@0, @1] afterDelay:0.9];
        });
}

+(void)showPopHint:(NSString *)contentMessage targetView:(UIView*)popView showInView:(UIView*)showView
{
    RXPopTipView *popTipView;
    popTipView = [[RXPopTipView alloc] initWithMessage:contentMessage];
    
    popTipView.backgroundColor = EastMarket_backgroundColor;
    popTipView.borderColor = EastMarket_bordColor;
    popTipView.textColor = EastMarket_darkGreyFontColor;
    popTipView.animation = CMPopTipAnimationSlide;//滑出还是弹出
    popTipView.has3DStyle = FALSE;
    popTipView.dismissTapAnywhere = YES;
    [popTipView autoDismissAnimated:YES atTimeInterval:1.0];
    
    [popTipView presentPointingAtView:popView inView:showView animated:YES];
}

/**
 *  手机号码验证
 *
 *  @param mobileNumbel 传入的手机号码
 *
 *  @return 格式正确返回true  错误 返回fals
 */
+ (BOOL) isMobile:(NSString *)mobileNumbel{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189,181(增加)
     */
    NSString * MOBIL = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189,181(增加)
     22         */
    NSString * CT = @"^1((33|53|8[019])[0-9]|349)\\d{7}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBIL];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNumbel]
         || [regextestcm evaluateWithObject:mobileNumbel]
         || [regextestct evaluateWithObject:mobileNumbel]
         || [regextestcu evaluateWithObject:mobileNumbel])) {
        return YES;
    }
    
    return NO;
}

+(NSString *) stringDeleteString:(NSString *)str
{
    NSMutableString *str1 = [NSMutableString stringWithString:str];
    for (int i = 0; i < str1.length; i++) {
        unichar c = [str1 characterAtIndex:i];
        NSRange range = NSMakeRange(i, 1);
        if (c == ',') { //此处可以是任何字符 (c == '"' || c == '.' || c == ',' || c == '(' || c == ')')
            [str1 deleteCharactersInRange:range];
            --i;
        }
    }
    NSString *newstr = [NSString stringWithString:str1];
    return newstr;
}

+(NSString* )getDateFormatterByMillisecond:(NSString* )millisecond{
    NSTimeInterval time = [millisecond doubleValue] + 28800;
    NSDate* detailDate = [NSDate dateWithTimeIntervalSince1970:time/1000.0];
    //实例化一个NSDateFormatter对象
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc]init];
    //设定时间格式，设置自己需要的时间格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString* currentDate = [dateFormatter stringFromDate:detailDate];
    return currentDate;
}


@end
